package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.entity.Month;
import com.example.entity.Result;
import com.example.service.MonthService;
import com.example.utils.ResultUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/month")
public class MonthController {
    @Autowired
    MonthService monthService;

    @GetMapping("/getTimeLine")
    public Result getTimeLine(HttpServletRequest request, HttpServletResponse response) {
        List<JSONObject> yearObjectList = monthService.getTimeLine();
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Credentials", "true");
        response.addHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
        response.addHeader("Content-type", "application/json");
        String header = request.getHeader("Access-Control-Request-Headers");
        if(header!=null) {
            response.addHeader("Access-Control-Allow-Headers", header);
        }
        return ResultUtil.success(yearObjectList);
    }

    @GetMapping("/selectById")
    public Result selectById(Long id, HttpServletResponse response) {
        Month monthNode = monthService.selectById(id);
        response.setHeader("Access-Control-Allow-Origin", "*");
        return ResultUtil.success(monthNode);
    }

    @GetMapping("/selectAll")
    public Result selectAll(HttpServletResponse response) {
        JSONObject jsonObject = monthService.selectAll();
        response.setHeader("Access-Control-Allow-Origin", "*");
        List<Month> monthList = (List<Month>)jsonObject.get("data");
        Collections.sort(monthList);
        return ResultUtil.success(monthList);
    }

    @PostMapping("/insert")
    public Result insert(HttpServletResponse response, @RequestBody Month month) {
        JSONObject jsonObject = monthService.insert(month);
        response.setHeader("Access-Control-Allow-Origin", "*");
        String msg = (String)jsonObject.get("msg");
        if (msg.equals("success")) {
            Month monthNode = (Month)jsonObject.get("data");
            return ResultUtil.success(monthNode);
        }
        return ResultUtil.error(500, msg);
    }

    @PostMapping("/update")
    public Result update(HttpServletResponse response, @RequestBody Month month) {
        JSONObject jsonObject = monthService.update(month);
        response.setHeader("Access-Control-Allow-Origin", "*");
        String msg = (String)jsonObject.get("msg");
        if (msg.equals("success")) {
            Month monthNode = (Month)jsonObject.get("data");
            return ResultUtil.success(monthNode);
        }
        return ResultUtil.error(500, msg);
    }

    @PostMapping("/deleteById")
    public Result deleteById(HttpServletResponse response, @RequestBody Month month) {
        JSONObject jsonObject = monthService.deleteById(month);
        response.setHeader("Access-Control-Allow-Origin", "*");
        String msg = (String)jsonObject.get("msg");
        if (msg.equals("success")) {
            Month monthNode = (Month)jsonObject.get("data");
            return ResultUtil.success(monthNode);
        }
        return ResultUtil.error(500, msg);
    }
}
